/* 获取公告数据 */
function getAnnAll(){
    // 发送ajax请求
    $.ajax({
        url:"http://localhost/lin/server/ann.php",
        success(msg){
            sessionStorage.setItem("annInfo",msg)
            msg = JSON.parse(msg).slice(0,10)
            renderAnnouInfo(msg);
            sessionStorage.setItem("pageCurrent",1)
        }
    })
}
getAnnAll()

/* 渲染页面 */
function renderAnnouInfo(msg){
    $("#anntd")[0].innerHTML = "";
    msg.forEach(item=>{
        let str = `
        <tr>
            <td>${item.nid}</td>
            <td>${item.ntitle}</td>
            <td>${item.ncontent}</td>
            <td>${item.ntime}</td>
            <td>
              <button data-bs-toggle="modal" data-bs-target="#myModalEdit" data-edit="edit" type="button" class="btn btn-outline" id="${item.nid}">编辑</button>
              <button type="button" class="btn btn-outline" data-del="del" id="${item.nid}" >删除</button>
            </td>
        </tr>
        `
        $("#anntd")[0].innerHTML += str
    })
}
function setTime1(){
    let d = new Date();
    let yea = d.getFullYear();
    let mon = d.getMonth()+1;
    let day = d.getDate();
    let hou = d.getHours();
    let min = d.getMinutes();
    return `${yea}年${mon}月${day}日${hou}时${min}分`
}
/* 添加公告 */
function addStudentInfo(){
/* 
    获取文本框内容
    进行内容效验
    点击添加获取当前时间
    发送ajax请求
    刷新页面
*/
    const title = $("#title").val()
    const comment = $("#comment").val()
    const sj = setTime1();
    console.log(sj);
    if(!(title && comment)){
        return alert("请输入标题和内容")
    }
    $.ajax({
        type:"POST",
        url:"http://localhost/lin/server/annadd.php",
        data:{ntitle:title,ncontent:comment,ntime:sj},
        success(){
            location.reload()
        }
    })
}

// 事件委派
$("#anntd").click(function(e){
    e = e || window.event
    /* 删除公告 */
    if(e.target.dataset.del == 'del'){
        const flag = window.confirm("你确定要删除吗？")
        if(flag){
            $.ajax({
                url:"http://localhost/lin/server/anndel.php",
                data:{id:e.target.id},
                success(){
                    location.reload();
                }
            })
        }
    }
    /* 
        修改公告
        1.打开模态框
        2.发送ajax请求获取数据
    */
    if(e.target.dataset.edit == 'edit'){
        $.ajax({
            url:"http://localhost/lin/server/annQueryOne.php",
            data:{id:e.target.id},
            success(msg1){
                msg1 = JSON.parse(msg1)
                console.log(msg1);
                $("#uptitle").val(msg1.ntitle);
                $("#upcomment").val(msg1.ncontent);
                $("#updateID").val(msg1.nid);
            }
        })
    }
})

function ok(){
    const title = $("#uptitle").val()
    const ncontent = $("#upcomment").val()
    const ntime = setTime1()
    const id = $('#updateID').val()
    $.ajax({
        type:"POST",
        url:"http://localhost/lin/server/annUpdate.php",
        data:{ntitle:title,ncontent:ncontent,ntime:ntime,id},
        success(){
            location.reload()
        }
    })
}

/* 分页 */
function first(){
    sessionStorage.setItem('pageCurrent',1)
    let annInfo = sessionStorage.getItem('annInfo')
    annInfo = JSON.parse(annInfo)
    annInfo = annInfo.slice(0,10)
    renderAnnouInfo(annInfo)
}

function last(){
    let annInfo = sessionStorage.getItem("annInfo")
    annInfo = JSON.parse(annInfo)
    const pageCurrent = Math.ceil(annInfo.length / 10)
    sessionStorage.setItem("pageCurrent",pageCurrent)
    if(annInfo.length % 10 != 0){
        annInfo = annInfo.slice(Math.floor(annInfo.length / 10) * 10,annInfo.length)
    }else{
        annInfo = annInfo.slice(Math.floor(annInfo.length / 10 - 1) * 10,annInfo.length)
    }
    renderAnnouInfo(annInfo)
}

function up(){
    let pageCurrent = +sessionStorage.getItem('pageCurrent')
    pageCurrent -= 1
    if(pageCurrent <= 1){
        pageCurrent = 1
    }
    sessionStorage.setItem('pageCurrent',pageCurrent)
    let annInfo = sessionStorage.get('annInfo')
    annInfo = JSON.parse(annInfo)
    annInfo = annInfo.slice((pageCurrent - 1) * 10 , pageCurrent)
    renderAnnouInfo(annInfo)
}

function down(){
    let pageCurrent = +sessionStorage.getItem('pageCurrent')
    pageCurrent += 1
    let annInfo = JSON.parse(sessionStorage.getItem('annInfo'))
    let lastPage = Math.ceil(annInfo.length / 10)
    if(pageCurrent > lastPage){
        pageCurrent = lastPage
    }
    sessionStorage.setItem('pageCurrent',pageCurrent)
    annInfo = annInfo.slice((pageCurrent - 1) * 10 , pageCurrent*10)
    renderAnnouInfo(annInfo)
}